<template>
  <div>
    <el-button type="success" @click="add">添加</el-button>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="部门id"></el-table-column>
      <el-table-column prop="name" label="部门名称"></el-table-column>
      <el-table-column prop="sn" label="部门编号"></el-table-column>
      <el-table-column label="操作">
        <!-- 作用域插槽<slot 自定义属性1="xxx" 自定义属性2="xxx"></slot> -->
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="edit(scope)">编辑</el-button>
          <el-button size="mini" type="danger" @click="del(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>
    <!-- 弹框组件 -->
    <my-dialog :title="dialogTitle" :visible="dialogFormVisible" :form="form" @close="closeDialog"/>
  </div>
</template>

<script>
// 引入弹框组件
import MyDialog from './children/Dialog'
import {
  getDepartments,
  addDepartment,
  editDepartment,
  delDepartment
} from "../../request/httpApi";
// import qs from "qs";
export default {
  components: {
    MyDialog
  },
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {},
      // formLabelWidth: "100px",
      dialogTitle: "",
      currentPage: 1, // 当前页码
      pageSize: 10, // 当前显示的条数
      total: 0
    };
  },
  methods: {
    closeDialog(reload) {
      this.dialogFormVisible = false
      if (reload == 'reload') {
        // 刷新
        this.fetchData();
      }
    },
    // 获取列表数据
    fetchData() {
      getDepartments({
        currentPage: this.currentPage,
        pageSize: this.pageSize
        // token: localStorage.getItem("token")
      }).then(res => {
        console.log(res);
        if (res.success) {
          console.log(res);
          this.tableData = res.data.list;
          // 总条数
          this.total = res.data.total;
        } else {
        }
      });
    },
    // 每页显示的条数发生改变触发
    handleSizeChange(size) {
      this.pageSize = size;
      // 重新获取数据
      this.fetchData();
    },
    // 页码发生改变后会触发
    handleCurrentChange(page) {
      console.log(page);
      this.currentPage = page;
      // 重新获取数据
      this.fetchData();
    },
    
    // 添加按钮
    add() {
      this.form = {};
      this.dialogTitle = "部门添加";
      this.dialogFormVisible = true;
    },
    // 编辑按钮
    edit(scope) {
      console.log(scope.row);
      //   this.form = JSON.parse(JSON.stringify(scope.row));
      this.dialogTitle = "部门编辑";
      this.form = { ...scope.row };
      this.dialogFormVisible = true;
    },
    del(scope) {
      this.$confirm("此操作将永久删除该部门, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delDepartment({
            // token: localStorage.getItem("token"),
            id: scope.row.id
          }).then(res => {
            if (res.success) {
              this.$message({
                type: "success",
                message: "删除部门成功!"
              });
              this.fetchData();
            }
          });
        })
        .catch(() => {});
    }
  },
  created() {
    this.fetchData()
  }
};
</script>

<style>
</style>